package com.m1905hd.user.web.controller

import com.aliyun.oss.OSSClient
import com.m1905hd.common.util.DateUtils
import com.m1905hd.user.utils.AliOSSUtil
import org.apache.commons.lang.RandomStringUtils
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.multipart.MultipartFile
import org.springframework.web.multipart.MultipartHttpServletRequest

import javax.servlet.http.HttpServletRequest

/**
 * Created with IDEA
 * User:hxh
 * Date:16/4/18
 * Time:下午1:45
 */
@Controller
class UploadController {

    @Autowired
    OSSClient client

    final static String BUCKET = "hdbox"

    final Logger logger = LoggerFactory.getLogger(UploadController.class)

    @RequestMapping(value = "/uploadImage.json", method = RequestMethod.POST)
    def uploadImage(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>()
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartRequest.getFile("Filedata")
        if (!multipartFile.isEmpty()) {
            try {
                String fileName = multipartFile.getOriginalFilename()
                def type = fileName.substring(fileName.lastIndexOf("."))
                def date = DateUtils.formatDate(new Date()).replaceAll("-", "") as String
                def created = Long.toString(System.currentTimeMillis()) as String
                def random = RandomStringUtils.random(6, false, true) as String
                def key = "img/" + date + "/" + created + random
                File temp = File.createTempFile(key, type)
                multipartFile.transferTo(temp)
                AliOSSUtil.uploadFile(client, BUCKET, key+type, temp)
                result.put("url", key+type)
                result.put("state", "SUCCESS")
            } catch (Exception e) {
                logger.error("上传失败")
            }
            return result;
        }
    }

    @RequestMapping(value = "/uploadVideo.json", method = RequestMethod.POST)
    def uploadVideo(MultipartFile upfile) {
        Map<String, Object> result = new HashMap<String, Object>()
        MultipartFile multipartFile = upfile
        if (!multipartFile.isEmpty()) {
            try {
                String fileName = multipartFile.getOriginalFilename()
                def type = fileName.substring(fileName.lastIndexOf("."))
                def date = DateUtils.formatDate(new Date()).replaceAll("-", "") as String
                def created = Long.toString(System.currentTimeMillis()) as String
                def random = RandomStringUtils.random(6, false, true) as String
                def key = "video/" + date + "/" + created + random
                File temp = File.createTempFile(key, type)
                multipartFile.transferTo(temp)
                AliOSSUtil.uploadFile(client, BUCKET, key+type, temp)
                result.put("url", key+type)
                result.put("state", "SUCCESS")
            } catch (Exception e) {
                logger.error("上传失败")
            }
            return result;
        }
    }
}
